package com.cheyunkeji.er.utils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * Created by Michael on 2017/8/14 21:40.
 */

public class KeyBoardUtil {

    /**
     * 上下文
     */
    public static Context appContext ;

    /**
     * 隐藏键盘
     *
     * @param isClearFoces 如果随意清空焦点会引发焦点被父控件获取，会导致上上层scroll类滑动
     * @param act
     */
    public static void dismissKeyboard(Activity act, boolean isClearFoces) {
        if (null == act)
            return;
        try {
            InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
            View focus = act.getCurrentFocus();
            if (null != focus) {
                imm.hideSoftInputFromWindow(focus.getWindowToken(), 0);
                if (isClearFoces) {
                    focus.clearFocus();
                }
            }
        } catch (Exception e) {
            Log.e("dismissKeyboard", e.toString());
        }
    }

    public static void dismissKeyboard(Activity act) {
        dismissKeyboard(act, true);
    }

    /**
     * 隐藏键盘
     *
     * @param view
     */
    public static void dismissKeyboard(View view) {
        if (null == view)
            return;
        try {
            InputMethodManager imm = (InputMethodManager) appContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            view.clearFocus();
        } catch (Exception e) {
            Log.e("dismissKeyboard", e.toString());
        }
    }

    public static boolean isKeyBoardShowing(EditText mEditText) {
        InputMethodManager inputMethodManager = (InputMethodManager) appContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager.isActive(mEditText)) {
            return true;
        }
        return false;
    }

    /**
     * 弹出键盘
     *
     * @param view
     */
    public static void showKeyboard(View view) {
        if (null == view)
            return;
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        InputMethodManager m = (InputMethodManager) appContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        m.showSoftInput(view, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 弹出键盘
     *
     * @param view
     */
    public static void showKeyboard(final EditText view) {
        if (null == view)
            return;
        view.requestFocus();
        view.postDelayed(new Runnable() {

            @Override
            public void run() {
                InputMethodManager inputMethodManager = (InputMethodManager) appContext
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.showSoftInput(view, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 500);
    }

    /**
     * 弹出键盘 -- 没有延迟
     *
     * @param view
     */
    public static void showKeyboardWithNoDelay(final EditText view) {
        if (null == view)
            return;
        view.requestFocus();
        view.setFocusableInTouchMode(true);
        InputMethodManager inputMethodManager = (InputMethodManager) appContext
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(view, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 键盘状态切换
     */
    public static void toggleSoftInput() {
        InputMethodManager m = (InputMethodManager) appContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
